package com.system.service.imp;

import com.common.Enums.ResponseEnum;
import com.common.exception.CourseException;
import com.common.util.CopyUtil;
import com.common.util.UuidUtil;
import com.core.system.domain.RoleResource;
import com.core.system.mapper.RoleResourceMapper;
import com.core.system.service.imp.RoleResourceServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
@Primary
public class RoleResourceExServiceImp   extends RoleResourceServiceImp {
    @Autowired
    private RoleResourceMapper roleResourceMapper;

    /**
     * 保存，id有值时更新，无值时新增
     */
    @Override
    public ResponseEntity save(com.core.system.dto.RoleResourceDto roleResourceDto) {
        RoleResource roleResource = CopyUtil.copy(roleResourceDto, RoleResource.class);
        if (StringUtils.isEmpty(roleResourceDto.getId())) {
           return this.insert(roleResource);
        } else {
            return this.update(roleResource);
        }
    }
    /**
     * 更新
     */
    private ResponseEntity update(RoleResource roleResource) {
        int i = roleResourceMapper.updateById(roleResource);
        if(i<=0){
            throw new CourseException(ResponseEnum.UPDATE_ERROR);
        }
        return ResponseEntity.ok().build();
    }
    /**
     * 新增
     */
    private ResponseEntity insert(RoleResource roleResource) {
        roleResource.setId(UuidUtil.getShortUuid());
        int i = roleResourceMapper.insert(roleResource);
        if(i<=0){
            throw new CourseException(ResponseEnum.ADD_ERROR);
        }
        return ResponseEntity.ok().build();
    }
}
